//
//  ViewController.m
//  3 - 23 GCD
//
//  Created by student on 16/3/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



void fun1()
{
    NSLog(@"%s:%@",__func__,[NSThread currentThread]);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);


    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_async(queue, ^{
        NSLog(@"%@",[NSThread currentThread]);
    });

    // 1. 执行方法 放入队列
    dispatch_async_f(queue, NULL, fun1);

    // 2. 延迟若干时间执行

    // 延迟3s在主线程执行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延迟了3秒 %@",[NSThread currentThread]);
    });

    // 基本等同于
    [self performSelector:@selector(foo) withObject:nil afterDelay:3];

    // 延迟4s在queue中执行 block
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"延迟了4秒 %@",[NSThread currentThread]);
    });

    // 延迟5s在queue中执行 函数
    dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), queue, NULL, fun1);

// 3. 只运行一次的代码
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        NSLog(@"这里面的代码只会运行一次!");
//    });

// 4. dispath_barrier与自定义的并发线程配合使用
//dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);// 自定义并发队列
//dispatch_async(queue, ^{
//    [NSThread sleepForTimeInterval:1];
//    NSLog(@"------------1----------------%@",[NSThread currentThread]);
//});
//dispatch_async(queue, ^{
//    [NSThread sleepForTimeInterval:1];
//    NSLog(@"------------2----------------%@",[NSThread currentThread]);
//});
//dispatch_barrier_async(queue, ^{
//    [NSThread sleepForTimeInterval:1];
//    NSLog(@"------------barrier----------------%@",[NSThread currentThread]);
//});
//dispatch_async(queue, ^{
//    [NSThread sleepForTimeInterval:1];
//    NSLog(@"------------3----------------%@",[NSThread currentThread]);
//});
//dispatch_barrier_async(queue, ^{
//    [NSThread sleepForTimeInterval:1];
//    NSLog(@"------------barrier----------------%@",[NSThread currentThread]);
//});
//dispatch_async(queue, ^{
//    [NSThread sleepForTimeInterval:1];
//    NSLog(@"------------4----------------%@",[NSThread currentThread]);
//});
//
}




- (void)foo
{
    NSLog(@"%s:%@",__func__,[NSThread currentThread]);
}
@end













